package com.kurt.controller;

import com.kurt.annotation.Log;
import com.kurt.common.PageResult;
import com.kurt.common.Result;
import com.kurt.dto.SysDictDataDTO;
import com.kurt.dto.create.SysDictDataCreateRequest;
import com.kurt.dto.query.SysDictDataQueryRequest;
import com.kurt.dto.update.SysDictDataUpdateRequest;
import com.kurt.entity.SysDictData;
import com.kurt.service.SysDictDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典数据控制器
 * 处理数据字典数据相关的HTTP请求
 */
@Slf4j
@RestController
@RequestMapping("/dict-datas")
@RequiredArgsConstructor
public class SysDictDataController extends BaseController {

    private final SysDictDataService dictDataService;

    /**
     * 创建数据字典数据
     *
     * @param createRequest 创建请求
     * @return 字典数据信息
     */
    @PostMapping
    @Log(title = "数据字典数据管理", businessType = 1, operatorType = 1)
    public Result<SysDictDataDTO> createDictData(@RequestBody SysDictDataCreateRequest createRequest) {
        try {
            createRequest.setCreateUserId(getCurrentUserId());
            log.info("创建数据字典数据请求: {}", createRequest.getDictLabel());
            SysDictDataDTO dictData = dictDataService.createDictData(createRequest);
            return Result.success("创建成功", dictData);
        } catch (Exception e) {
            log.error("创建数据字典数据失败: {}", e.getMessage(), e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新数据字典数据
     *
     * @param updateRequest 更新请求
     * @return 字典数据信息
     */
    @PutMapping
    @Log(title = "数据字典数据管理", businessType = 2, operatorType = 1)
    public Result<SysDictDataDTO> updateDictData(@RequestBody SysDictDataUpdateRequest updateRequest) {
        try {
            updateRequest.setUpdateUserId(getCurrentUserId());
            log.info("更新数据字典数据请求: {}", updateRequest.getId());
            SysDictDataDTO dictData = dictDataService.updateDictData(updateRequest);
            return Result.success("更新成功", dictData);
        } catch (Exception e) {
            log.error("更新数据字典数据失败: {}", e.getMessage(), e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取字典数据详情
     *
     * @param id 字典数据ID
     * @return 字典数据信息
     */
    @GetMapping("/{id}")
    public Result<SysDictData> getDictDataById(@PathVariable("id") Long id) {
        try {
            log.info("获取字典数据详情请求: {}", id);
            SysDictData dictData = dictDataService.getById(id);
            return Result.success("获取成功", dictData);
        } catch (Exception e) {
            log.error("获取字典数据详情失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询数据字典数据
     *
     * @param queryRequest 查询请求
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<PageResult<SysDictDataDTO>> getDictDataPage(@RequestBody SysDictDataQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("分页查询数据字典数据请求: {}", queryRequest);
            Page<SysDictDataDTO> dictDataPage = dictDataService.getDictDataPage(queryRequest);

            // 转换为PageResult
            PageResult<SysDictDataDTO> pageResult = PageResult.of(
                    queryRequest.getPageNum(),
                    queryRequest.getPageSize(),
                    dictDataPage.getTotalElements(),
                    dictDataPage.getContent()
            );

            return Result.success("分页查询成功", pageResult);
        } catch (Exception e) {
            log.error("分页查询数据字典数据失败: {}", e.getMessage(), e);
            return Result.error("分页查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询数据字典数据列表
     *
     * @param queryRequest 查询请求
     * @return 字典数据列表
     */
    @PostMapping("/list")
    public Result<List<SysDictDataDTO>> getDictDataList(@RequestBody SysDictDataQueryRequest queryRequest) {
        try {
            queryRequest.setLoginUserId(getCurrentUserId());
            log.info("查询数据字典数据列表请求: {}", queryRequest);
            List<SysDictDataDTO> dictDatas = dictDataService.getDictDataList(queryRequest);
            return Result.success("查询成功", dictDatas);
        } catch (Exception e) {
            log.error("查询数据字典数据列表失败: {}", e.getMessage(), e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据字典类型ID获取字典数据列表
     *
     * @param dictTypeId 字典类型ID
     * @return 字典数据列表
     */
    @GetMapping("/type-id/{dictTypeId}")
    public Result<List<SysDictDataDTO>> getDictDataByTypeId(@PathVariable("dictTypeId") Long dictTypeId) {
        try {
            log.info("根据字典类型ID获取字典数据列表请求: {}", dictTypeId);
            List<SysDictDataDTO> dictDatas = dictDataService.getDictDataByTypeId(dictTypeId);
            return Result.success("获取成功", dictDatas);
        } catch (Exception e) {
            log.error("根据字典类型ID获取字典数据列表失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 根据字典类型获取字典数据列表
     *
     * @param dictType 字典类型
     * @return 字典数据列表
     */
    @GetMapping("/type/{dictType}")
    public Result<List<SysDictDataDTO>> getDictDataByType(@PathVariable("dictType") String dictType) {
        try {
            log.info("根据字典类型获取字典数据列表请求: {}", dictType);
            List<SysDictDataDTO> dictDatas = dictDataService.getDictDataByType(dictType);
            return Result.success("获取成功", dictDatas);
        } catch (Exception e) {
            log.error("根据字典类型获取字典数据列表失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 根据字典类型获取启用的字典数据列表
     *
     * @param dictType 字典类型
     * @return 字典数据列表
     */
    @GetMapping("/type/{dictType}/enabled")
    public Result<List<SysDictDataDTO>> getEnabledDictDataByType(@PathVariable("dictType") String dictType) {
        try {
            log.info("根据字典类型获取启用的字典数据列表请求: {}", dictType);
            List<SysDictDataDTO> dictDatas = dictDataService.getEnabledDictDataByType(dictType);
            return Result.success("获取成功", dictDatas);
        } catch (Exception e) {
            log.error("根据字典类型获取启用的字典数据列表失败: {}", e.getMessage(), e);
            return Result.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 删除数据字典数据
     *
     * @param id 字典数据ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Log(title = "数据字典数据管理", businessType = 3, operatorType = 1)
    public Result<Boolean> deleteDictData(@PathVariable("id") Long id) {
        try {
            log.info("删除数据字典数据请求: {}", id);
            boolean success = dictDataService.deleteById(id);
            return Result.success("删除成功", success);
        } catch (Exception e) {
            log.error("删除数据字典数据失败: {}", e.getMessage(), e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除数据字典数据
     *
     * @param ids 字典数据ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    @Log(title = "数据字典数据管理", businessType = 3, operatorType = 1)
    public Result<Boolean> deleteDictDatas(@RequestBody List<Long> ids) {
        try {
            log.info("批量删除数据字典数据请求: {}", ids);
            boolean success = dictDataService.deleteDictDatas(ids);
            return Result.success("批量删除成功", success);
        } catch (Exception e) {
            log.error("批量删除数据字典数据失败: {}", e.getMessage(), e);
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 根据字典类型ID删除字典数据
     *
     * @param dictTypeId 字典类型ID
     * @return 删除结果
     */
    @DeleteMapping("/type-id/{dictTypeId}")
    @Log(title = "数据字典数据管理", businessType = 3, operatorType = 1)
    public Result<Boolean> deleteDictDataByTypeId(@PathVariable("dictTypeId") Long dictTypeId) {
        try {
            log.info("根据字典类型ID删除字典数据请求: {}", dictTypeId);
            boolean success = dictDataService.deleteDictDataByTypeId(dictTypeId);
            return Result.success("删除成功", success);
        } catch (Exception e) {
            log.error("根据字典类型ID删除字典数据失败: {}", e.getMessage(), e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }
}
